package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetMealMapper setMealMapper;

    @Autowired
    ShopCartMapper shopCartMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断是否已经在购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> list= shopCartMapper.select(shoppingCart);
        if(list!=null && !list.isEmpty()){
            ShoppingCart shoppingCartSelect =  list.get(0);
            //已经在购物车，增加数量
            shoppingCartSelect.setNumber(shoppingCartSelect.getNumber()+1);
            shopCartMapper.update(shoppingCartSelect);
        }else {
            ShoppingCart shoppingCartSelect;
            if (shoppingCartDTO.getDishId()!=null){
                shoppingCartSelect = new ShoppingCart();
                BeanUtils.copyProperties(shoppingCartDTO,shoppingCartSelect);
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCartSelect.setName(dish.getName());
                shoppingCartSelect.setImage(dish.getImage());
                shoppingCartSelect.setAmount(dish.getPrice());
            }else{
                shoppingCartSelect = new ShoppingCart();
                BeanUtils.copyProperties(shoppingCartDTO,shoppingCartSelect);
                Setmeal setmeal = setMealMapper.getSetMealById(shoppingCartSelect.getSetmealId());
                shoppingCartSelect.setName(setmeal.getName());
                shoppingCartSelect.setImage(setmeal.getImage());
                shoppingCartSelect.setAmount(setmeal.getPrice());
            }
            shoppingCartSelect.setNumber(1);
            shoppingCartSelect.setUserId(BaseContext.getCurrentId());
            shoppingCartSelect.setCreateTime(LocalDateTime.now());
            shopCartMapper.insert(shoppingCartSelect);
        }

    }

    @Override
    public List<ShoppingCart> list() {
        long user_id = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(user_id).build();
        List <ShoppingCart> list =  shopCartMapper.select(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        long userId = BaseContext.getCurrentId();
        shopCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中商品
     * @param shoppingCartDTO
     */
    public void sub(ShoppingCartDTO shoppingCartDTO){
        shopCartMapper.deleteByDishOrSetmeal(shoppingCartDTO);
    }
}
